package cn.wangzg.course0603;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import cn.wangzg.course0603.adapter.CommonAdapter;
import cn.wangzg.course0603.adapter.ViewHolder;
import cn.wangzg.course0603.bean.Article;
import cn.wangzg.course0603.util.HttpCallbackModelListener;
import cn.wangzg.course0603.util.HttpUrlConnectionUtils;

public class MainActivity extends AppCompatActivity {
    private ListView listArticle;
    private View footer;

    private CommonAdapter<Article.DataBean.DatasBean> adapter;
    //分页加载使用
    private int mTotalItemCount;//item的总数
    private int mLastVisibleItemIndex;//当前可见的最后一项
    private boolean isLoading=false;//是否正在加载，避免重复加载
    private int curPage=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initData() {
        HttpUrlConnectionUtils.doGet(this,
                "https://www.wanandroid.com/article/list/0/json",
                new HttpCallbackModelListener() {
                    @Override
                    public void onFinish(Object response) {
                        adapter.setmDatas(((Article) response).getData().getDatas());
                        curPage++;
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                }, Article.class);
    }

    private void initView() {
        //handler = new MyHandler(this);
        footer = getLayoutInflater().inflate(R.layout.footer, null);
        listArticle = findViewById(R.id.listArticle);
        //使用通用适配器，建议大家掌握，但是考试时不考此内容
        adapter = new CommonAdapter<Article.DataBean.DatasBean>(this, null,
                R.layout.item_article) {
            @Override
            public void convert(ViewHolder helper, final Article.DataBean.DatasBean item) {
                helper.setText(R.id.textView, item.getTitle());
                helper.setText(R.id.textView3, item.getShareUser());
                helper.setText(R.id.textView5, item.getSuperChapterName()
                        + "/" + item.getChapterName());
                helper.setText(R.id.textView7, item.getNiceDate());
                helper.getView(R.id.imageView).setSelected(item.isCollect());
                helper.getView(R.id.imageView).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        v.setSelected(!v.isSelected());
                        item.setCollect(v.isSelected());
                    }
                });
            }
        };
        listArticle.setAdapter(adapter);
        listArticle.addFooterView(footer);
        footer.setVisibility(View.GONE);
        //添加单击事件


        /**
         * 添加分页加载
         * 1.判断是否到达底部
         * 2.加载数据，更新ListView
         */
        listArticle.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //当item总数和可见的最后一个Item索引相等时，并且状态为空闲，则开始加载下一页
                //scrollState有三种状态，分别是开始滚动（SCROLL_STATE_FLING），
                // 正在滚动(SCROLL_STATE_TOUCH_SCROLL), 已经停止（SCROLL_STATE_IDLE）
                if (mTotalItemCount == mLastVisibleItemIndex && scrollState == SCROLL_STATE_IDLE) {
                    if (!isLoading) {//避免重复加载
                        isLoading = true;
                        //footer.findViewById(R.id.control_layout_footer).setVisibility(View.VISIBLE);
                        footer.setVisibility(View.VISIBLE);
                        // 调用接口中的回调方法进行数据更新
                        HttpUrlConnectionUtils.doGet(MainActivity.this,
                                "https://www.wanandroid.com/article/list/"+curPage+"/json",
                                new HttpCallbackModelListener() {
                                    @Override
                                    public void onFinish(Object response) {
                                        adapter.addmDatas(((Article) response).getData().getDatas());
                                        curPage++;
                                        isLoading=false;
                                        footer.setVisibility(View.GONE);
                                    }

                                    @Override
                                    public void onError(Exception e) {

                                    }
                                }, Article.class);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                //记录当前可见的最后一个Item的索引
                mLastVisibleItemIndex = firstVisibleItem + visibleItemCount;
                //记录Item总数
                mTotalItemCount = totalItemCount;
            }
        });
    }
}
